﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Globalization;

namespace DisplayBoardInfo.Client.Forms
{
    public delegate void EditedDelegate(object oSender, EventArgs oEventArgs);

    public partial class EditPatient : Form
    {
        private Boolean closed;

        public event EditedDelegate EditingDone;

        public void Edit(string sFirstName, string sLastName, DateTime dAppointmentTime, int sVisitTime, string sPesel)
        {
            ChangingArgs oEditingArgs = new ChangingArgs(sFirstName, sLastName, dAppointmentTime, sVisitTime, sPesel);
            FireEditingEvent(oEditingArgs);
        }

        public void FireEditingEvent(EventArgs oEventArgs)
        {
            if (null != EditingDone)
            {
                EditingDone(this, oEventArgs);
            }
        }

        public EditPatient(string sFirstName, string sLastName, 
            string sAppointmentName, int sVisitTime, string sPesel)
        {
            InitializeComponent();
            closed = false;
            appoimentDTP.Format = DateTimePickerFormat.Custom;
            appoimentDTP.CustomFormat = "yyyy.MM.dd HH:mm";
            appoimentDTP.ShowUpDown = true;

            string pattern = "yyyy.MM.dd HH:mm";
            DateTime parsedDate;
            if (DateTime.TryParseExact(sAppointmentName, pattern, null, DateTimeStyles.None, out parsedDate))
                this.appoimentDTP.Value = parsedDate;
            else
                this.appoimentDTP.Value = DateTime.Now;
            this.firstNameTB.Text = sFirstName;
            this.lastNameTB.Text = sLastName;
            this.visitTimeUD.Value = sVisitTime;
            this.peselTB.Text = sPesel;
        }

        private void editButton_Click(object sender, EventArgs e)
        {
            if (firstNameTB.Text != "" && lastNameTB.Text != "" &&
                appoimentDTP != null && visitTimeUD.Value != null && peselTB.Text != "")
            {
                if (peselTB.Text.Count() == 11)
                {
                    Edit(firstNameTB.Text, lastNameTB.Text, appoimentDTP.Value, (Int32)visitTimeUD.Value, peselTB.Text);
                    this.closed = true;
                    this.Close();
                }
                else
                {
                    MessageBox.Show("PESEL powinien się składać z 11 cyfr.", "Błąd dodawania pacjenta",
                    MessageBoxButtons.OK, MessageBoxIcon.Warning,
                    MessageBoxDefaultButton.Button1);
                }
            }
            else
            {
                MessageBox.Show("Wszystkie pola są wymagane.", "Błąd edycji danych pacjenta",
                    MessageBoxButtons.OK, MessageBoxIcon.Warning,
                    MessageBoxDefaultButton.Button1);
            }
        }

        private void cancelButton_Click(object sender, EventArgs e)
        {
            closed = true;
            this.Close();
        }

        public Boolean isClosed
        {
            get { return this.closed; }
        }

        private void editPatient_FormClosed(object sender, FormClosedEventArgs e)
        {
            closed = true;
        }
    }
}
